﻿using MaiPlanServer.IServices.IQualityServices;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.QualityManage
{
    /// <summary>
    /// 常见缺陷
    /// </summary>
    [Route("api/[controller]")]
    public class DefectController : Controller
    {
        public DefectController(IDefectServices dsdb)
        {
            Dsdb = dsdb;
        }

        public IDefectServices Dsdb { get; }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        /// <param name="PageSize">每页显示条数</param>
        /// <param name="Describe">缺陷描述</param>
        /// <param name="Type">检测项类型</param>
        /// <param name="Level">缺陷等级</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetJson(int PageIndex, int PageSize, string Describe, string Type, string Level)
        {
            try
            {
                var list = await Dsdb.GetJsonAsync(PageIndex, PageSize, Describe, Type, Level);
                var count = await Dsdb.GetCountAsync(Describe, Type, Level);
                return Ok(BaseState.CreateInstance(200, "请求成功", list, count));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败"));
            }
        }

        [HttpPost]
        public async Task<IActionResult> Add([FromForm] qc_defect model)
        {
            try
            {
                model.defect_id = Guid.NewGuid().ToString();
                model.create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                model.defect_code = "qc-"+ DateTime.Now.ToString("yyyy-MM-dd");
                var result = await Dsdb.AddAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "请求成功"));
                }
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常，请联系管理员"));
            }
        }

        [HttpDelete]
        public async Task<IActionResult> Del(string Id)
        {
            try
            {
                var result = await Dsdb.DelAsync(Id);
                return Ok(BaseState.CreateInstance(200, "请求成功"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败"));
            }
        }

        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll(string Id)
        {
            try
            {
                string[] ids = Id.Split(',');
                var result = await Dsdb.DelAsync(ids);
                return Ok(BaseState.CreateInstance(200, "请求成功"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败"));
            }
        }
        [HttpPut]
        public async Task<IActionResult> Update([FromForm] qc_defect model)
        {
            try
            {
                model.update_time = DateTime.Now.ToString("yyyy-MM=dd HH:mm:ss");
                model.update_by = "";
                var result = await Dsdb.UpdateAsync(model);
                return Ok(BaseState.CreateInstance(200,"请求成功"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError,"请求失败"));
            }
        }
    }
}
